/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ToolbarCustomize.java
 *
 * Created on 22 Ιουλ 2010, 9:24:44 μμ
 */
package myComponents.myToolbar;

import java.awt.Component;
import java.awt.Dimension;
import java.util.logging.Level;
import javax.swing.JDialog;
import myComponents.MyUsefulFunctions;
import tools.MySeriesLogger;
import tools.options.MySeriesOptions;

/**
 *
 * @author ssoldatos
 */
public class ToolbarCustomize extends JDialog {

  private CustomizingToolbar ctoolbar;
  public Integer[] newVisibleButtons;

  public ToolbarCustomize(Integer[] visibleButtons) {
    Toolbar.customize = true;
    setModalityType(ModalityType.APPLICATION_MODAL);
    ctoolbar = new CustomizingToolbar(visibleButtons);
    newVisibleButtons = visibleButtons;
    MySeriesLogger.logger.log(Level.INFO, "Initializing components");
    initComponents();
    MySeriesLogger.logger.log(Level.FINE, "Components initialized");
    setLocationRelativeTo(null);
    myseriesproject.MySeries.glassPane.activate(null);
    setVisible(true);
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    inner_panel = new javax.swing.JPanel();
    panelUsed = new javax.swing.JPanel();
    panelUnused = new javax.swing.JPanel();
    bt_ok = new myComponents.myGUI.buttons.MyButtonOk();
    bt_add = new myComponents.myGUI.buttons.MyButtonAdd();
    bt_remove = new myComponents.myGUI.buttons.MyButtonRemove();
    bt_cancel = new myComponents.myGUI.buttons.MyButtonCancel();

    setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    setResizable(false);
    setUndecorated(true);

    inner_panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
    inner_panel.setMaximumSize(new java.awt.Dimension(600, 600));

    panelUsed.setBorder(javax.swing.BorderFactory.createTitledBorder("Used Buttons"));
    panelUsed.setMaximumSize(new java.awt.Dimension(800, 32767));
    panelUsed.setOpaque(false);
    addUsedButtons();
    panelUsed.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEADING, 2, 2));

    panelUnused.setBorder(javax.swing.BorderFactory.createTitledBorder("Unused buttons"));
    panelUnused.setMaximumSize(new java.awt.Dimension(800, 32767));
    addUnusedButtons();
    panelUnused.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEADING, 2, 2));

    bt_ok.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bt_okActionPerformed(evt);
      }
    });

    bt_add.setToolTipText("Add all buttons");
    bt_add.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bt_addActionPerformed(evt);
      }
    });

    bt_remove.setToolTipText("Remove all buttons");
    bt_remove.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bt_removeActionPerformed(evt);
      }
    });

    bt_cancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bt_cancelActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout inner_panelLayout = new javax.swing.GroupLayout(inner_panel);
    inner_panel.setLayout(inner_panelLayout);
    inner_panelLayout.setHorizontalGroup(
      inner_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, inner_panelLayout.createSequentialGroup()
        .addGroup(inner_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
          .addGroup(javax.swing.GroupLayout.Alignment.LEADING, inner_panelLayout.createSequentialGroup()
            .addComponent(bt_add, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(bt_remove, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGroup(inner_panelLayout.createSequentialGroup()
            .addContainerGap()
            .addGroup(inner_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
              .addComponent(panelUnused, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 676, javax.swing.GroupLayout.PREFERRED_SIZE)
              .addComponent(panelUsed, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 676, javax.swing.GroupLayout.PREFERRED_SIZE))))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(bt_ok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, inner_panelLayout.createSequentialGroup()
        .addContainerGap(692, Short.MAX_VALUE)
        .addComponent(bt_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    );
    inner_panelLayout.setVerticalGroup(
      inner_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(inner_panelLayout.createSequentialGroup()
        .addComponent(bt_cancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(5, 5, 5)
        .addComponent(panelUsed, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
        .addComponent(panelUnused, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(inner_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
          .addComponent(bt_remove, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(bt_add, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(bt_ok, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(inner_panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(inner_panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void addRemove() {
    ctoolbar.buttons.clear();
    ctoolbar.createButtons();
    ctoolbar.addButtons();
    MySeriesLogger.logger.log(Level.INFO, "Removing all unused panel buttons");
    panelUnused.removeAll();
    MySeriesLogger.logger.log(Level.INFO, "Removing all used panel buttons");
    panelUsed.removeAll();
    addUsedButtons();
    addUnusedButtons();
  }

  private void bt_addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_addActionPerformed
    MySeriesLogger.logger.log(Level.INFO, "Setting all buttons as visible");
    ctoolbar.visibleButtons = MySeriesOptions.getDefaultToolbarButtons();
    addRemove();
  }//GEN-LAST:event_bt_addActionPerformed

  private void bt_removeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_removeActionPerformed
    MySeriesLogger.logger.log(Level.INFO, "Setting no buttons as visible");
    ctoolbar.visibleButtons = new Integer[]{};
    addRemove();
  }//GEN-LAST:event_bt_removeActionPerformed

  private void bt_okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_okActionPerformed
    getNewVisibleButtons();
    Toolbar.customize = false;
    myseriesproject.MySeries.glassPane.deactivate();
    MySeriesLogger.logger.log(Level.INFO, "Closing window");
    dispose();
  }//GEN-LAST:event_bt_okActionPerformed

  private void bt_cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelActionPerformed
    myseriesproject.MySeries.glassPane.deactivate();
    Toolbar.customize = false;
    MySeriesLogger.logger.log(Level.INFO, "Closing window");
    dispose();

  }//GEN-LAST:event_bt_cancelActionPerformed
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private myComponents.myGUI.buttons.MyButtonAdd bt_add;
  private myComponents.myGUI.buttons.MyButtonCancel bt_cancel;
  private myComponents.myGUI.buttons.MyButtonOk bt_ok;
  private myComponents.myGUI.buttons.MyButtonRemove bt_remove;
  private javax.swing.JPanel inner_panel;
  private javax.swing.JPanel panelUnused;
  private javax.swing.JPanel panelUsed;
  // End of variables declaration//GEN-END:variables

  private void addUsedButtons() {
    MySeriesLogger.logger.log(Level.INFO, "Adding visible buttons to used panel");
    for (int i = 0; i < ctoolbar.visibleButtons.length; i++) {
      Integer key = ctoolbar.visibleButtons[i];
      if (ctoolbar.buttons.containsKey(key)) {
        Component b = ctoolbar.buttons.get(key);
        if (isNotDeprecated(b)) {
        b.setPreferredSize(new Dimension(29, 29));
        b.addMouseMotionListener(new ToolbarButtonMouseMotionListener(panelUsed, panelUnused));
        b.addMouseListener(new ToolbarButtonMouseListener(panelUsed, panelUnused));
        panelUsed.add(b);
        }
      }
    }
    panelUsed.validate();
    panelUsed.repaint();
  }

  private void addUnusedButtons() {
    MySeriesLogger.logger.log(Level.INFO, "Adding not visible buttons to unused panel");
    for (int i = 0; i < ctoolbar.buttons.size(); i++) {
      Component b = ctoolbar.buttons.get(i);
      if (isNotDeprecated(b)) {
        if (!MyUsefulFunctions.isInArray(ctoolbar.visibleButtons, i)) {
          b.setPreferredSize(new Dimension(29, 29));
          b.addMouseMotionListener(new ToolbarButtonMouseMotionListener(panelUsed, panelUnused));
          b.addMouseListener(new ToolbarButtonMouseListener(panelUsed, panelUnused));
          panelUnused.add(b);
        }
      }
    }
    panelUnused.validate();
    panelUnused.repaint();
  }

  private void getNewVisibleButtons() {
    MySeriesLogger.logger.log(Level.INFO, "Getting visible buttons");
    Component[] comps = panelUsed.getComponents();
    newVisibleButtons = new Integer[comps.length];
    for (int i = 0; i < comps.length; i++) {
      Component c = comps[i];
      if (c instanceof ToolbarButton) {
        ToolbarButton t = (ToolbarButton) c;
        newVisibleButtons[i] = t.getActionName();
      } else {
        ToolbarSeperator t = (ToolbarSeperator) c;
        newVisibleButtons[i] = t.getActionName();
      }
    }
    //Arrays.sort(newVisibleButtons);
  }

  private boolean isNotDeprecated(Component b) {
    if(b instanceof ToolbarButton){
      ToolbarButton tb = (ToolbarButton)b;
      return !tb.isDeprecated();
    }
    return true;
  }

  class CustomizingToolbar extends AbstractToolbar {

    private static final long serialVersionUID = 2211414141L;

    public CustomizingToolbar() {
    }

    public CustomizingToolbar(Integer[] visibleButtons) {
      createButtons();
      addButtons();
      this.visibleButtons = visibleButtons;
    }

    @Override
    protected void populateToolbar() {
    }
  }
}
